<template>
  <a-modal
    title="操作"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    footer=""
  >
    <a-steps :current="current" style="visibility: hidden">
      <a-step v-for="item in steps" :key="item.title" :title="item.title" />
    </a-steps>

    <a-form  v-if="steps[current].content==0">
      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="充值金额"
      >
        <a-input placeholder="充值金额"
                   v-model="setmoney"  v-decorator="[ 'money', {rules: [{ required: true,len:'3',max:'3', message: '请输入充值金额(仅限数字)',pattern: new RegExp(/^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0.\d{1,2}$/)}]}]"/>
      </a-form-item>
    </a-form>
    <a-form-item  v-if="steps[current].content==0" :labelCol="labelCol" :wrapperCol="wrapperCol" label="支付方式" >
      <a-radio-group  @change="onChange" v-decorator="[ 'status', {rules: [{ required: true, message: '请选择' }]}]">
        <a-radio :value="1">微信</a-radio>
        <a-radio :value="2">支付宝</a-radio>
      </a-radio-group>

    </a-form-item>
    <a-form-item  v-if="steps[current].content==1">
      <div   id="qrcode" ref="qrCodeUrl"></div>
    </a-form-item>


    <div class="steps-action">
      <a-button v-if="current < steps.length - 1" type="primary" @click="next" style="position: absolute;right:50%;bottom: 2%">
      下一步
      </a-button>
      <a-button v-if="current>0" style="position: absolute;right:56%;bottom: 2%" @click="prev">
        上一步
      </a-button>
      <a-button
        v-if="current == steps.length - 1"
        type="primary"
        @click="handleOk"
        style="position: absolute;right:34%;bottom: 2%"
      >
        关闭
      </a-button>
    </div>

  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {wxScanPay,aliScanPay} from  '@/api/businessService/full'
  import QRCode from 'qrcodejs2'
  export default {
    data(){
      return{
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        form: this.$form.createForm(this),
        visible: false,
        confirmLoading: false,
        current:0,
        text:'',
        setmoney:'',
        type:'',
        steps:[
          {
            title:'填写金额',
            content:'0'
          },
          {
            title:'二维码',
            content:'1'
          }

        ]
      }
    },
    methods:{
      add() {
        this.isDisabled = false
        this.visible = true
      },
      next() {
      if(this.type && this.setmoney){
        console.log(111)
        this.current++;
      }else {
        return
      }
         if(this.type == 1){
          let valus ={
            businessId:this.mdl.id,
            totalFee: this.setmoney*100
          }
          wxScanPay(valus).then((res)=>{
            let text = res.result
            this.QRcode(text);
          })
        }else {
           let valus ={
             businessId:this.mdl.id,
             totalFee: this.setmoney*100
           }
          aliScanPay(valus).then((res)=>{
            let text = res.result
            this.QRcode(text);
          })
        }
      },
      prev() {
        this.type =''
        this.current--;
        if(this.type == 1){
          let valus ={
            businessId:this.mdl.id,
            totalFee: this.setmoney*100
          }
          wxScanPay(valus).then((res)=>{
            let text = res.result
            this.QRcode(text);
          })
        }else {
          let valus ={
            businessId:this.mdl.id,
            totalFee: this.setmoney*100
          }
          aliScanPay(valus).then((res)=>{
            let text = res.result
            this.QRcode(text);
          })
        }
      },
      onChange(e){
        this.type = e.target.value

      },
      QRcode(text){
        let qrcode = new QRCode(this.$refs.qrCodeUrl,{
          text:text,
          width: 200,
          height: 200,
          colorDark: '#000000', //前景色
          colorLight: '#ffffff', //背景色
          correctLevel: QRCode.CorrectLevel.H
        })
      },
      edit(record) {
        console.log('edit',record)
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl,'status'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
      },
      handleOk() {
        this.close()
        this.current=0
      },
      handleCancel() {
        this.close()
      },
    },

  }
</script>

<style scoped lang="less">
#qrcode{
  height: 200px;
  width: 200px;
  margin:20px auto;
}
</style>